package com.xywy.bloodandroid.act;

import java.util.Calendar;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.NumberPicker.Formatter;
import android.widget.NumberPicker.OnScrollListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.Remind;
import com.xywy.bloodandroid.broadcast.RemindService;
import com.xywy.bloodandroid.util.FileUtils;
import com.xywy.bloodandroid.widget.MyDialog;

/*******************************************
 * @Title: EditRemindActivty.java
 * @Package com.xywy.bloodandroid.act
 * @Description: TODO添加编辑提醒
 * @author Benwolf
 * @date 2014-12-29 下午2:53:04
 * @version V1.0
 ********************************************/
public class EditRemindActivty extends BaseActivity {

	private RelativeLayout back;
	private TextView title;
	private TextView right_title;
	private TextView tv_time;
	private TimePicker tp_remind;
	long time;
	private EditText et_medicine_name;
	private EditText et_dosage;
	private Remind remind = new Remind();

	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_edit_remind);
		back = (RelativeLayout) findViewById(R.id.left_img);
		title = (TextView) findViewById(R.id.middle_title);
		right_title = (TextView) findViewById(R.id.right_title);
		right_title.setVisibility(View.VISIBLE);
		tp_remind = (TimePicker) findViewById(R.id.tp_remind);
		et_medicine_name = (EditText) findViewById(R.id.et_medicine_name);
		et_dosage = (EditText) findViewById(R.id.et_dosage);
		tp_remind.setIs24HourView(true);
		tp_remind.setOnTimeChangedListener(new OnTimeChangedListener() {

			@Override
			public void onTimeChanged(TimePicker view, int hour, int minute) {
				// TODO Auto-generated method stub
				// time = hour + ":" + minute;
				Calendar c = Calendar.getInstance();
				c.set(Calendar.HOUR, hour);
				c.set(Calendar.MINUTE, minute);
				time = c.getTimeInMillis();

			}
		});
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		back.setOnClickListener(this);
		right_title.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.left_img:
			Intent mIntent = new Intent();
			mIntent.putExtra("time", time);
			mIntent.putExtra("medicine_name", et_medicine_name.getText()
					.toString());
			mIntent.putExtra("dosage", et_dosage.getText().toString());
			System.out.println("========" + time
					+ et_medicine_name.getText().toString()
					+ et_dosage.getText().toString());
			this.setResult(1, mIntent);
			finish();
			break;
		case R.id.right_title:
			if (et_medicine_name.getText().toString().equals("")) {
				Toast.makeText(context, "药品名称不能为空！", Toast.LENGTH_SHORT).show();
				return;
			} else {
				List<Remind> reminds = FileUtils.getList("reminds");
				remind.setTime(time);
				remind.setYaoname(et_medicine_name.getText().toString());
				remind.setDosage(et_dosage.getText().toString());
				reminds.add(remind);
				FileUtils.saveObjectList("reminds", reminds);
				Log.e("remind", FileUtils.getList("reminds").size()+"");
				startAlarm();
				finish();
			}
			break;

		default:
			break;
		}

	}

	void startAlarm(){
		Intent intent = new Intent(this,RemindService.class);
		startService(intent);
	}
	
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		title.setText(getResources().getString(R.string.add_remind_str));
		right_title.setText(getResources().getString(R.string.save_str));
	}

}
